﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Cheb
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            textBox2.Text = "4";
            textBox3.Text = "4";
            textBox4.Text = "3";
            textBox5.Text = "1e-8";
            textBox6.Text = "200";
        }

        public double uu(double x, double y)
        {
            double tmp = Math.Exp(1 - x*x - y*y);
            return tmp;
        }
        public double right(double y)
        {
            double tmp = uu(1,y);
            return tmp;
        }
        public double left(double y)
        {
            double tmp = uu(-1,y);
            return tmp;
        }
        public double top(double x)
        {
            double tmp = uu(x,1);
            return tmp;
        }
        public double down(double x)
        {
            double tmp = uu(x,-1);
            return tmp;
        }
        public double funk(double x, double y)
        {
            return 4 * uu(x, y) * (x * x + y * y - 1);
        }
        private void button1_Click(object sender, EventArgs e)
        {
            
            int n;
            int.TryParse(textBox2.Text,out n);
            int m;
            int.TryParse(textBox3.Text, out m);
            int l;
            int.TryParse(textBox4.Text, out l);
            double ee;
            double.TryParse(textBox5.Text, out ee);
            int s;
            int.TryParse(textBox6.Text, out s);


            double h = 2.0/n;
            double k = 2.0/m;
            double[,] u = new double[n + 1, m + 1];
            for (int i = 1; i < n; i++)
            {
                u[i, 0] = down(-1 + h * i);
                u[i, m] = top(-1 + h * i);
            }
            for (int i = 1; i < m; i++)
            {
                u[0, i] = left(-1 + k * i);
                u[n, i] = right(-1 + k * i);
            }
            
            double[,] b = new double[(n + 1) , (m + 1)];
            for (int i = 1; i < n; i++)
            {
                for (int j = 1; j < m; j++)
                {
                    b[i,j] = funk(-1 + i * h, -1 + j * k); 
                }
            }

            ChebMethod met = new ChebMethod(l, n, h, m, k, b, u);
            int Imax=0;
            int Jmax=0;
            int counter = 0;
            double ereal = 0;
            counter = met.method(ee, out ereal, s, Imax, Jmax);

            //оценка точности решения
            Imax = 1;
            Jmax = 1;
            for (int i = 1; i < n; i++)
            {
                for (int j = 1; j < m; j++)
                {
                    if(Math.Abs( uu(-1 + i*h, -1 +j*k) - met.u[i,j] ) > Math.Abs( uu(-1 + Imax*h, -1 +Jmax*k) - met.u[Imax,Jmax] ))
                    {
                        Imax = i;
                        Jmax = j;
                    }
                }
            }
            //

            textBox1.Text = counter.ToString() + Environment.NewLine + ereal.ToString()
                + Environment.NewLine + Math.Abs(uu(-1 + Imax * h, -1 + Jmax * k) - met.u[Imax, Jmax]).ToString();


        }

       
    }
}
